#!/bin/sh
#
# This is the script for generating files for a specific Dalton test job.
#
# For the .check file ksh or bash is preferred, otherwise use sh
# (and hope it is not the old Bourne shell, which will not work)
#
if [ -x /bin/ksh ]; then
   CHECK_SHELL='#!/bin/ksh'
elif [ -x /bin/bash ]; then
   CHECK_SHELL='#!/bin/bash'
else
   CHECK_SHELL='#!/bin/sh'
fi


#######################################################################
#  TEST DESCRIPTION
#######################################################################
cat > geoopt_prop.info <<'%EOF%'
   geoopt_prop
   -----------
   Molecule:         H2O
   Wave Function:    SCF / 6-311++G**
   Test Purpose:     Geometry optimization using .OPTIMIZE module
                     (1st order) with a calculation of quadrupole moment,
                     magnetizability and nuclear shieldings at the
                     optimized geometry, along with a vibrational analysis
%EOF%

#######################################################################
#  MOLECULE INPUT
#######################################################################
cat > geoopt_prop.mol <<'%EOF%'
BASIS
6-311++G**
Geometry optimization, calculation of quadrupole moment, magnetizabilities
and nuclear shieldings at the optimized geometry
    2
        8.    1
O     0.0 -0.2249058930 0.0
        1.    2
H     1.45235 0.899623 0.0
H    -1.45235 0.899623 0.0
%EOF%

#######################################################################
#  DALTON INPUT
#######################################################################
cat > geoopt_prop.dal <<'%EOF%'
**DALTON INPUT
.OPTIMIZE
**WAVE FUNCTION
.HF
**PROPERTIES
.QUADRU
.VIBANA
.MAGNET
.SHIELD
**END OF DALTON INPUT
%EOF%
#######################################################################



#######################################################################

#######################################################################
#  CHECK SCRIPT
#######################################################################
echo $CHECK_SHELL >geoopt_prop.check
cat >>geoopt_prop.check <<'%EOF%'
log=$1

if [ `uname` = Linux ]; then
   GREP="egrep -a"
else
   GREP="egrep"
fi

# Reading molecular geometry:
CRIT1=`$GREP "1 * x * (0| )\.0000000000" $log | wc -l`
CRIT2=`$GREP "2 * y * (0| )\.0000000000" $log | wc -l`
CRIT3=`$GREP "3 * z * (\-0|\-)\.1258515023" $log | wc -l`
CRIT4=`$GREP "4 * x * (0| )\.0000000000" $log | wc -l`
CRIT5=`$GREP "5 * y * 1\.4523500000" $log | wc -l`
CRIT6=`$GREP "6 * z * (0| )\.9986773907" $log | wc -l`
CRIT7=`$GREP "7 * x * (0| )\.0000000000" $log | wc -l`
CRIT8=`$GREP "8 * y * \-1\.4523500000" $log | wc -l`
CRIT9=`$GREP "9 * z * (0| )\.9986773907" $log | wc -l`
CRIT10=`$GREP "Total number of coordinates: * 9" $log | wc -l`
TEST[1]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3  \+ $CRIT4  \+ $CRIT5  \+ \
              $CRIT6  \+ $CRIT7  \+ $CRIT8  \+ $CRIT9  \+ $CRIT10`
CTRL[1]=10
ERROR[1]="MOLECULAR GEOMETRY NOT READ CORRECTLY"

# Check 1st order geometry optimization:
CRIT1=`$GREP "1st Order Geometry Optimization" $log | wc -l`
CRIT2=`$GREP "BFGS update" $log | wc -l`
CRIT3=`$GREP "performed in redundant internal coordinates" $log | wc -l`
CRIT4=`$GREP "Model Hessian will be used as initial Hessian" $log | wc -l`
CRIT5=`$GREP "Trust region method will be used to control step" $log | wc -l`
TEST[2]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3  \+ $CRIT4  \+ $CRIT5`
CTRL[2]=5
ERROR[2]="GEOMETRY OPTIMIZATION NOT INITIALIZED CORRECTLY"

# Symmetry:
CRIT1=`$GREP "Number of coordinates in each symmetry: * 3 * 2 * 3 * 1" $log | wc -l`
CRIT2=`$GREP "Number of orbitals in each symmetry: * 17 * 6 * 11 * 2" $log | wc -l`
TEST[3]=`expr $CRIT1  \+ $CRIT2`
CTRL[3]=2
ERROR[3]="SYMMETRY IS NOT CORRECT"

# SCF energy, 1st iteration:
CRIT1=`$GREP "Total energy * \-76\.051459519[0-9] au \(Hartrees\)" $log | wc -l`
CRIT2=`$GREP "\-2069\.4654847[0-9] eV" $log | wc -l`
CRIT3=`$GREP "\-199673\.076[0-9] kJ\/mol" $log | wc -l`
TEST[4]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[4]=3
ERROR[4]="INITIAL ENERGY NOT CORRECT"

# Relativistic corrections:
CRIT1=`$GREP "Darwin correction\: *               0*\.1974514... au" $log | wc -l`
CRIT2=`$GREP "Mass\-velocity correction\: *     \-0*\.2489637... au" $log | wc -l`
CRIT3=`$GREP "Total relativistic correction\: * \-0*\.0515123... au \(\ (0| )\.0677\%\)" $log | wc -l`
CRIT4=`$GREP "Non\-relativistic \+ relativistic energy\: * \-76\.1029718... au" $log | wc -l`
TEST[5]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4`
CTRL[5]=4
ERROR[5]="RELATIVISTIC CORRECTIONS NOT CORRECT"

# Gradient, 1st iteration:
CRIT1=`$GREP "O * z * \-0*\.04102" $log | wc -l`
CRIT2=`$GREP "H * y *   0*\.04951" $log | wc -l`
CRIT3=`$GREP "H * z *   0*\.04102" $log | wc -l`
CRIT4=`$GREP "O *      0*\.0000000000 *   0*\.0000000000 * \-0*\.04102" $log | wc -l`
CRIT5=`$GREP "H * _1 * 0*\.0000000000 *   0*\.02475..... *   0*\.02051" $log | wc -l`
CRIT6=`$GREP "H * _2 * 0*\.0000000000 * \-0*\.02475..... *   0*\.02051" $log | wc -l`
TEST[6]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6`
CTRL[6]=6
ERROR[6]="INITIAL GRADIENT NOT CORRECT"

# Dipole moment, 1st iteration:
CRIT1=`$GREP "0*\.88703. * 2\.25462. * 7\.5206" $log | wc -l`
TEST[7]=`expr $CRIT1`
CTRL[7]=1
ERROR[7]="INITIAL DIPOLE MOMENT NOT CORRECT"

# Step, 1st iteration:
CRIT1=`$GREP "O *      0*\.0000000000 *  0*\.0000000000 * \-0*\.10366" $log | wc -l`
CRIT2=`$GREP "H * _1 * 0*\.0000000000 *   1\.41819..... *   0*\.98758" $log | wc -l`
CRIT3=`$GREP "H * _2 * 0*\.0000000000 * \-1\.41819..... *   0*\.98758" $log | wc -l`
TEST[8]=`expr $CRIT1  \+ $CRIT2 \+ $CRIT3`
CTRL[8]=3
ERROR[8]="INITIAL STEP NOT CORRECT"

# 2nd iteration:
CRIT1=`$GREP "Energy at this geometry is * \: * \-76\.05331" $log | wc -l`
CRIT2=`$GREP "Norm of gradient * \: * 0*\.01366" $log | wc -l`
CRIT3=`$GREP "Norm of step *     \: * 0*\.0209[56]" $log | wc -l`
TEST[9]=`expr $CRIT1  \+ $CRIT2 \+ $CRIT3`
CTRL[9]=3
ERROR[9]="SECOND ITERATION NOT CORRECT"

# 3rd iteration:
CRIT1=`$GREP "Energy at this geometry is * \: * \-76\.05341" $log | wc -l`
CRIT2=`$GREP "Norm of gradient * \: * 0*\.00207" $log | wc -l`
CRIT3=`$GREP "Norm of step *     \: * 0*\.0102" $log | wc -l`
TEST[10]=`expr $CRIT1  \+ $CRIT2 \+ $CRIT3`
CTRL[10]=3
ERROR[10]="THIRD ITERATION NOT CORRECT"

# Final geometry:
CRIT1=`$GREP -l "O *      0*\.0000000000 *  0*\.000000* *\-0*\.0879[67]" $log | wc -l`
CRIT2=`$GREP -l "H * _1 * 0*\.0000000000 *   1\.42256.*    0*\.97973" $log | wc -l`
CRIT3=`$GREP -l "H * _2 * 0*\.0000000000 * \-1\.42256.*    0*\.97973" $log | wc -l`
TEST[11]=`expr $CRIT1  \+ $CRIT2 \+ $CRIT3`
CTRL[11]=3
ERROR[11]="FINAL GEOMETRY NOT CORRECT"

# Final SCF energy:
CRIT1=`$GREP "Total energy * \-76\.05342355[0-9][0-9] au \(Hartrees\)" $log | wc -l`
TEST[12]=`expr $CRIT1`
CTRL[12]=1
ERROR[12]="FINAL ENERGY NOT CORRECT"

# Quadrupole moment:
CRIT1=`$GREP "Ex *   \-1\.75566. * \-*0*\.000000 * \-*0*\.000000" $log | wc -l`
CRIT2=`$GREP "Ey * \-*0*\.000000 * 1\.9385.. * \-*0*\.000000" $log | wc -l`
CRIT3=`$GREP "Ez * \-*0*\.000000 * \-*0*\.000000 * \-0*\.1828.." $log | wc -l`
TEST[13]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[13]=3
ERROR[13]="QUADRUPOLE MOMENT NOT CORRECT"

# Magnetizability:
CRIT1=`$GREP "Isotropic magnetizability\: * \-2\.8986 * \-228\.731. * \-13\.7745" $log | wc -l`
CRIT2=`$GREP "Diamagnetic contribution\: *  \-3\.2053 * \-252\.9(29|30). * \-15\.2318" $log | wc -l`
CRIT3=`$GREP "Paramagnetic contribution\: *  0*\.3067 *    24\.19[89]. *    1\.4573" $log | wc -l`
CRIT4=`$GREP "1st anisotropy\: * (\-0|\-)\.0339 * \-2\.67.. * (\-0|\-)\.1611" $log | wc -l`
CRIT5=`$GREP "2nd anisotropy\: * (0| )\.0367 * 2\.893. * (0| )\.174" $log | wc -l`
CRIT6=`$GREP "Bx * \-2\.90046.* * (\-0| 0| \-|  )\.000000000000 * (\-0| 0| \-|  )\.000000000000" $log | wc -l`
CRIT7=`$GREP "By * (\-0| 0| \-|  )\.000000000000 * \-2\.87417.* (\-0| 0| \-|  )\.000000000000" $log | wc -l`
CRIT8=`$GREP "Bz * (\-0| 0| \-|  )\.000000000000 * (\-0| 0| \-|  )\.000000000000 * \-2\.92121" $log | wc -l`
TEST[14]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3  \+ $CRIT4  \+ $CRIT5  \+ \
               $CRIT6  \+ $CRIT7  \+ $CRIT8`
CTRL[14]=8
ERROR[14]="MAGNETIZABILITIES NOT CORRECT"

# Molecular rotational g-factors:
CRIT1=`$GREP "Isotropic g-tensor      * \: * (0| )\.68204" $log | wc -l`
CRIT2=`$GREP "Nuclear contribution    * \: * (0| )\.97479" $log | wc -l`
CRIT3=`$GREP "Diamagnetic contribution *\: * (0| )\.0250." $log | wc -l`
CRIT4=`$GREP "Electronic contribution * \: * (\-0|\-)\.31778" $log | wc -l`
CRIT5=`$GREP "1st anisotropy\: * (\-0|\-)\.01875" $log | wc -l`
CRIT6=`$GREP "2nd anisotropy\: *    (0| )\.07291" $log | wc -l`
CRIT7=`$GREP "A *    0*\.6695.*  \-*0*\.00000000 * 0*\.00000000" $log | wc -l`
CRIT8=`$GREP "B * \-*0*\.00000000 * 0*\.73065.*    0*\.00000000" $log | wc -l`
CRIT9=`$GREP "C *    0*\.00000000 * 0*\.00000000 * 0*\.64593" $log | wc -l`
TEST[15]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3  \+ $CRIT4  \+ $CRIT5  \+ \
               $CRIT6  \+ $CRIT7  \+ $CRIT8  \+ $CRIT9`
CTRL[15]=9
ERROR[15]="ROTATIONAL G-TENSOR NOT CORRECT"

# Chemical shielding:
CRIT1=`$GREP -l "Shielding constant\: * 334\.25.. ppm" $log | wc -l`
CRIT2=`$GREP -l "Anisotropy\: *  53\.06.. ppm" $log | wc -l`
CRIT3=`$GREP -l "Asymmetry\: *   0*\.323" $log | wc -l`
CRIT4=`$GREP -l "S parameter\: * 53\.98.. ppm" $log | wc -l`
CRIT5=`$GREP -l "A parameter\: * 0*\.0000 ppm" $log | wc -l`
CRIT6=`$GREP -l "Shielding constant\: * 31\.863. ppm" $log | wc -l`
CRIT7=`$GREP -l "Anisotropy\: * 20\.546. ppm" $log | wc -l`
CRIT8=`$GREP -l "Asymmetry\: * 0*\.0968" $log | wc -l`
CRIT9=`$GREP -l "S parameter\: * 20\.578. ppm" $log | wc -l`
CRIT10=`$GREP -l "A parameter\: * 0*\.6772 ppm" $log | wc -l`
CRIT11=`$GREP -l "\@1 *O *     334\.25.. * 417\.275. *\-83\.02.. * 0*\.611. * 58\.78.. * 53\.06.. * 0*\.323" $log | wc -l`
CRIT12=`$GREP -l "\@1 *H * _1 * 31\.8639 *  24\.922. *   6\.9410 * 0*\.8750 * 21\.209. * 20\.546. * 0*\.096" $log | wc -l`
CRIT13=`$GREP -l "\@1 *H * _2 * 31\.8639 *  24\.922. *   6\.9410 * 0*\.8750 * 21\.209. * 20\.546. * 0*\.096" $log | wc -l`
TEST[16]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3  \+ $CRIT4  \+ $CRIT5  \+ \
               $CRIT6  \+ $CRIT7  \+ $CRIT8  \+ $CRIT9  \+ $CRIT10 \+ \
               $CRIT11 \+ $CRIT12 \+ $CRIT13`
CTRL[16]=13
ERROR[16]="SHIELDINGS NOT CORRECT"

# Vibrational frequencies:
CRIT1=`$GREP "2 * A1 * 4142\.5. * 0*\.01887. * 25\.48. * 0*\.603" $log | wc -l`
CRIT2=`$GREP "3 * A1 * 1726\.2. * 0*\.00786. * 85\.6.. * 2\.026" $log | wc -l`
CRIT3=`$GREP "1 * B2 * 4244\.5. * 0*\.0193.. * 88\.2.. * 2\.089" $log | wc -l`
TEST[17]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3`
CTRL[17]=3
ERROR[17]="FREQUENCIES / IR INTENSITIES NOT CORRECT"

# Geometry optimization:
CRIT1=`$GREP "Geometry converged in * 5 * iterations\!" $log | wc -l`
CRIT2=`$GREP "Energy at final geometry is * \: * \-76\.053424 a\.u\." $log | wc -l`
CRIT3=`$GREP "Energy change during optimization \: * \-0*\.001964 a\.u\." $log | wc -l`
TEST[18]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3`
CTRL[18]=3
ERROR[18]="GEOMETRY OPTIMIZATION FAILED"

PASSED=1
for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
do
   if [ ${TEST[i]} -ne ${CTRL[i]} ]; then
     echo "${ERROR[i]} ( test = ${TEST[i]}; control = ${CTRL[i]} )"
     PASSED=0
   fi
done

if [ $PASSED -eq 1 ]
then
  echo TEST ENDED PROPERLY
  exit 0
else
  echo THERE IS A PROBLEM
  exit 1
fi

%EOF%
#######################################################################
